/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testdesign;

//Java Libraries
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.testis.utils.middleware.GlobalDataHandler;

//ATOF libraries.
import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.model.testdesign.ConditionTextEditorModel;
import org.testis.gui.mvc.view.testdesign.ConditionsTextEditorView;
import org.testis.testdriver.TestPlanGlobalsHashMap;
import org.testis.testdriver.translation.AtofValueTranslateService;
import org.testis.testdriver.translation.UnidentifiedTranslationException;

/**
 *
 * @author barisergun
 */
public class ConditionTextEditorController extends AbstractController {

    public static final String CHANGE_CONDITION_TXT = "ConditionText";
    public static final String CHANGE_CONDITION_ID = "ConditionId";
    public static final String CHANGE_EVALUATED_TXT = "EvaluatedText";
    public static final String CHANGE_EVALUATION_RESULT_TXT = "EvaluationResult";
    public static final String CHANGE_EVALUATION_BOOLEAN = "Evaluatable";
    private static ConditionTextEditorController m_SingleController;
    private ConditionTextEditorModel m_CondTxtModel;
    private ConditionsTextEditorView m_CondTxtView;

    private ConditionTextEditorController() {
        super();
    }

    public static ConditionTextEditorController createInstance() {
        if (m_SingleController == null) {
            m_SingleController = new ConditionTextEditorController();
        }
        return m_SingleController;
    }

    public void operationClose() {
        cleanAll();
        m_SingleController = null;

    }

    public void initialize(Integer condId, String condString) {
        m_CondTxtModel = new ConditionTextEditorModel();
        addModel(m_CondTxtModel);
        m_CondTxtView = new ConditionsTextEditorView(TestDesignControllerList.newInstance().
                getParentFrame(), true);
        addView(m_CondTxtView);
        if (condString != null) {
            operationSetConditionText(condString);
        }
        if (condId != null) {
            operationSetConditionId(condId);
        }

    }

    public void operationProfileList() {
        TestDesignControllerList.newInstance().getProfileSettingDetailsController().
                useForCondition();
    }

    public void operationLanguageList() {
        TestDesignControllerList.newInstance().getLanguageVariablesController().
                startLoader();
    }

    public void operationFocusEvaluationGained(String condString) {
        Integer profileId = TestDesignControllerList.newInstance().
                getProfileSettingDetailsController().getSelectedProfileId();
        Integer langId = TestDesignControllerList.newInstance().
                getLanguageVariablesController().getSelectedLanguageId();
        HashMap<String, String> languageMap = TestDesignControllerList.newInstance().
                getLanguageHandlerController().operationParseLanguagePairs(langId, false);
        GlobalDataHandler handler = new GlobalDataHandler();
        HashMap<String, String> profileMap = handler.getProfileDetailsAsMap(profileId);

        AtofValueTranslateService service = new AtofValueTranslateService(
                profileMap, languageMap, null, null);

        String translated = "";
        try {
            translated = service.translateStaticVariables(condString);
        } catch (UnidentifiedTranslationException ex) {
            translated = ex.getMessage();
        }
        TestPlanGlobalsHashMap.createInstance().resetMaps();
        setModelProperty(CHANGE_EVALUATED_TXT, translated);

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine jsEngine = manager.getEngineByName("js");
        String result = "";
        Boolean evaluatable = true;

        try {
            result = jsEngine.eval(translated).toString();

        } catch (ScriptException ex) {
            evaluatable = false;
            result = ex.getMessage();

        }
        setModelProperty(CHANGE_EVALUATION_RESULT_TXT, result);
        setModelProperty(CHANGE_EVALUATION_BOOLEAN, evaluatable);

    }

    public void operationApplyConditionText(Integer condTextId,
            String condString) {
        ConditionsController.getInstance().
                operationAddItemToList(condTextId, condString);
    }

    public void operationSetConditionText(String condstring) {
        setModelProperty(CHANGE_CONDITION_TXT, condstring);
    }

    public String operationGetConditionText() {
        return (String) getModelProperty(CHANGE_CONDITION_TXT);
    }

    public void operationSetConditionId(Integer condId) {
        setModelProperty(CHANGE_CONDITION_ID, condId);
    }

    public Integer operationGetConditionId() {
        return (Integer) getModelProperty(CHANGE_CONDITION_ID);
    }
    // Getters

    public ConditionsTextEditorView getConditioTextEditorView() {
        return m_CondTxtView;
    }
    // Setters
}
